//
//  SHYCommonTool
//  Pods
//
//  Created by sunhaiyan on 16/6/28.
//
//

#import "NSStringWithColor.h"

@implementation NSStringWithColor
//ios6以下处理
+(NSString *)getNewString:(NSString *)str
{
//    NSInteger fromIndex = 0;
//    NSInteger toIndex = 0;
//    NSInteger fromSecIndex = 0;
//    NSInteger toSecIndex = 0;
    if (str.length==0)
        return nil;
    
    NSMutableString *mStr    = [[NSMutableString alloc] initWithString:str];
    
//    fromIndex = 0;
    for( int i=0;i< mStr.length;i++ )
    {
        NSString *str = [mStr substringWithRange:NSMakeRange(i, 1)];
        if( [str isEqualToString:@"{" ] )
        {
//            fromIndex = i;
            [mStr replaceCharactersInRange:NSMakeRange(i, 1) withString:@""];
            break;
        }
    }
    
//    toIndex = 0;
    for( int i=0;i< mStr.length;i++ )
    {
        NSString *str = [mStr substringWithRange:NSMakeRange(i, 1)];
        if( [str isEqualToString:@"}" ] )
        {
//            toIndex = i;
            [mStr replaceCharactersInRange:NSMakeRange(i, 1) withString:@""];
            break;
        }
    }
    
//    fromSecIndex = 0;
    for( int i=0;i< mStr.length;i++ )
    {
        NSString *str = [mStr substringWithRange:NSMakeRange(i, 1)];
        if( [str isEqualToString:@"{" ] )
        {
//            fromSecIndex = i;
            [mStr replaceCharactersInRange:NSMakeRange(i, 1) withString:@""];
            break;
        }
    }
    
    for( int i=0;i< mStr.length;i++ )
    {
        NSString *str = [mStr substringWithRange:NSMakeRange(i, 1)];
        if( [str isEqualToString:@"}" ] )
        {
//            toSecIndex = i;
            [mStr replaceCharactersInRange:NSMakeRange(i, 1) withString:@""];
            break;
        }
    }
    return mStr;
}

//ios6以上处理显示文字
+(NSMutableAttributedString *)getAttibutedString:(NSString *)str lightColor:(UIColor *)lColor normalColor:(UIColor *)nColor font:(UIFont *)afont;
{
    if (str.length==0)
        return nil;
    
    NSMutableString *mStr    = [[NSMutableString alloc] initWithString:str];
    NSInteger fromIndex = 0;
    
    
    for( int i=0;i< mStr.length;i++ )
    {
        NSString *str = [mStr substringWithRange:NSMakeRange(i, 1)];
        if( [str isEqualToString:@"{" ] )
        {
            fromIndex = i;
            [mStr replaceCharactersInRange:NSMakeRange(fromIndex, 1) withString:@""];
            break;
        }
    }
    
    NSInteger toIndex = 0;
    for( int i=0;i< mStr.length;i++ )
    {
        NSString *str = [mStr substringWithRange:NSMakeRange(i, 1)];
        if( [str isEqualToString:@"}" ] )
        {
            toIndex = i;
            [mStr replaceCharactersInRange:NSMakeRange(toIndex, 1) withString:@""];
            break;
        }
    }
    
    NSInteger fromIndexSec = 0;
    for( int i=0;i< mStr.length;i++ )
    {
        NSString *str = [mStr substringWithRange:NSMakeRange(i, 1)];
        if( [str isEqualToString:@"{" ] )
        {
            fromIndexSec = i;
            [mStr replaceCharactersInRange:NSMakeRange(fromIndexSec, 1) withString:@""];
            break;
        }
    }
    
    NSInteger toIndexSec = 0;
    for( int i=0;i< mStr.length;i++ )
    {
        NSString *str = [mStr substringWithRange:NSMakeRange(i, 1)];
        if( [str isEqualToString:@"}" ] )
        {
            toIndexSec = i;
            [mStr replaceCharactersInRange:NSMakeRange(toIndexSec, 1) withString:@""];
            break;
        }
    }
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:mStr];

    if (toIndex!=0) {
        [attStr addAttribute:NSForegroundColorAttributeName
                       value:nColor
                       range:NSMakeRange(0, fromIndex)];
        
        [attStr addAttribute:NSForegroundColorAttributeName
                       value:lColor
                       range:NSMakeRange(fromIndex, (toIndex-fromIndex))];
        if (toIndexSec!=0) {
            [attStr addAttribute:NSForegroundColorAttributeName
                           value:nColor
                           range:NSMakeRange(toIndex, fromIndexSec-toIndex)];
            [attStr addAttribute:NSForegroundColorAttributeName
                           value:lColor
                           range:NSMakeRange(fromIndexSec, (toIndexSec-fromIndexSec))];
            [attStr addAttribute:NSForegroundColorAttributeName
                           value:nColor
                           range:NSMakeRange(toIndexSec, attStr.length-toIndexSec)];
            
        }else{
            [attStr addAttribute:NSForegroundColorAttributeName
                           value:nColor
                           range:NSMakeRange(toIndex, attStr.length-toIndex)];
        }
        
        
    }else{
        [attStr addAttribute:NSForegroundColorAttributeName
                       value:nColor
                       range:NSMakeRange(0, attStr.length)];
    }
    
    [attStr addAttribute:NSFontAttributeName value:afont range:NSMakeRange(0, attStr.length)];
    
    return attStr;
}

+(NSMutableAttributedString *)getAttibutedString:(NSString *)str lightColor:(UIColor *)lColor normalColor:(UIColor *)nColor lightFont:(UIFont *)lfont normalFont:(UIFont *)nfont{
    
    NSMutableString *mStr    = [[NSMutableString alloc] initWithString:str];
    NSInteger fromIndex = 0;
    
    
    for( int i=0;i< mStr.length;i++ )
    {
        NSString *str = [mStr substringWithRange:NSMakeRange(i, 1)];
        if( [str isEqualToString:@"{" ] )
        {
            fromIndex = i;
            [mStr replaceCharactersInRange:NSMakeRange(fromIndex, 1) withString:@""];
            break;
        }
    }
    
    NSInteger toIndex = 0;
    for( int i=0;i< mStr.length;i++ )
    {
        NSString *str = [mStr substringWithRange:NSMakeRange(i, 1)];
        if( [str isEqualToString:@"}" ] )
        {
            toIndex = i;
            [mStr replaceCharactersInRange:NSMakeRange(toIndex, 1) withString:@""];
            break;
        }
    }
    
    NSInteger fromIndexSec = 0;
    for( int i=0;i< mStr.length;i++ )
    {
        NSString *str = [mStr substringWithRange:NSMakeRange(i, 1)];
        if( [str isEqualToString:@"{" ] )
        {
            fromIndexSec = i;
            [mStr replaceCharactersInRange:NSMakeRange(fromIndexSec, 1) withString:@""];
            break;
        }
    }
    
    NSInteger toIndexSec = 0;
    for( int i=0;i< mStr.length;i++ )
    {
        NSString *str = [mStr substringWithRange:NSMakeRange(i, 1)];
        if( [str isEqualToString:@"}" ] )
        {
            toIndexSec = i;
            [mStr replaceCharactersInRange:NSMakeRange(toIndexSec, 1) withString:@""];
            break;
        }
    }
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:mStr];
    
    if (toIndex!=0) {
        //普通文案 字体
        [attStr addAttribute:NSForegroundColorAttributeName
                       value:nColor
                       range:NSMakeRange(0, fromIndex)];
        [attStr addAttribute:NSFontAttributeName value:nfont range:NSMakeRange(0, fromIndex)];
        //高亮文案 字体
        [attStr addAttribute:NSForegroundColorAttributeName
                       value:lColor
                       range:NSMakeRange(fromIndex, (toIndex-fromIndex))];
        
        [attStr addAttribute:NSFontAttributeName value:lfont range:NSMakeRange(fromIndex, (toIndex-fromIndex))];

        
        if (toIndexSec!=0) {
            //普通字体
            [attStr addAttribute:NSForegroundColorAttributeName
                           value:nColor
                           range:NSMakeRange(toIndex, fromIndexSec-toIndex)];
            [attStr addAttribute:NSFontAttributeName value:nfont range:NSMakeRange(toIndex, fromIndexSec-toIndex)];
            //高亮字体
            [attStr addAttribute:NSForegroundColorAttributeName
                           value:lColor
                           range:NSMakeRange(fromIndexSec, (toIndexSec-fromIndexSec))];
            
            [attStr addAttribute:NSFontAttributeName value:lfont range:NSMakeRange(fromIndexSec, (toIndexSec-fromIndexSec))];
            
            //普通字体
            [attStr addAttribute:NSForegroundColorAttributeName
                           value:nColor
                           range:NSMakeRange(toIndexSec, attStr.length-toIndexSec)];
            
            [attStr addAttribute:NSFontAttributeName value:nfont range:NSMakeRange(toIndexSec, attStr.length-toIndexSec)];

            
        }else{
            [attStr addAttribute:NSForegroundColorAttributeName
                           value:nColor
                           range:NSMakeRange(toIndex, attStr.length-toIndex)];
            
            [attStr addAttribute:NSFontAttributeName value:nfont range:NSMakeRange(toIndex, attStr.length-toIndex)];
        }
    }else{
        [attStr addAttribute:NSForegroundColorAttributeName
                       value:nColor
                       range:NSMakeRange(0, attStr.length)];
    }
    return attStr;
}
@end
